Skip to content

Conversation

josuah
Copy link
Contributor

@josuah josuah commented Jul 16, 2025

Dependency:

The USB class was doing arbitrary "best guess" choices about what pixel formats or resolutions to include (all of them, and min/max only when format are ranges).

  • In the class, add a uvc_add_format() to allow the application to push the exact format it wants
  • In the class, remove all format-related video API calls away from the UVC stack, to let the application handle this
  • In the sample, filter out formats that are not supported by the host whenever standard formats are also supported
  • In the sample, filter out formats that are too large for the memory

What is added in the sample is a matter of application choices, and trapping these choices in the UVC class prevented the samples to work without manual configuration on diverse platform.

MacOS support still not confirmed.
Linux, Windows, Android tested functional with the hardware I have.

@josuah josuah marked this pull request as draft July 16, 2025 10:22
@github-actions github-actions bot added area: USB Universal Serial Bus area: Samples Samples labels Jul 16, 2025
@github-actions github-actions bot requested a review from nashif July 16, 2025 10:23
@josuah
Copy link
Contributor Author

josuah commented Jul 16, 2025

This is not ready for review, will be properly split in individual commits, and "un-drafted" when ready...

@josuah josuah force-pushed the pr_uvc_no_policy branch from 70ab814 to 4833717 Compare July 18, 2025 21:48
@josuah josuah changed the title [WIP] UVC: remove the policies from the class UVC: remove the policies from the class Jul 18, 2025
@josuah
Copy link
Contributor Author

josuah commented Jul 18, 2025

Force-push:

  • Rebase on top of the MacOSX-related bugfix

Windows, Linux, MacOSX, Android are now expected to all work.

Tested with nRF52840-DK:

west build --board=nrf52840dk/nrf52840 --snippet=video-sw-generator samples/subsys/usb/uvc/

Tested with Arduino Nicla Vision:

west build --board=arduino_nicla_vision/stm32h747xx/m7 samples/subsys/usb/uvc/

@josuah josuah added area: Video Video subsystem Release Notes Required Release notes required for this change labels Jul 18, 2025
@josuah josuah changed the title UVC: remove the policies from the class UVC: move application decision to the application Jul 19, 2025
@josuah josuah force-pushed the pr_uvc_no_policy branch 3 times, most recently from f1cb80e to f5328dd Compare July 24, 2025 16:23
@josuah
Copy link
Contributor Author

josuah commented Jul 24, 2025

Force-push:

  • Rebase on main to fix conflict.
  • Reword the documentation and commit messages.
  • Add a migration guide entry for the semantic change (but no direct API breakage).

Copy link

@josuah josuah marked this pull request as ready for review July 24, 2025 16:56
@zephyrbot zephyrbot added the Release Notes To be mentioned in the release notes label Jul 24, 2025
@zephyrbot zephyrbot requested review from cfriedt and jhedberg July 24, 2025 16:57
tmon-nordic
tmon-nordic previously approved these changes Sep 15, 2025
Copy link
Contributor

@tmon-nordic tmon-nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs rebase.

@josuah
Copy link
Contributor Author

josuah commented Oct 11, 2025

Force-push:

  • Apply review suggestions
  • Check compliance script typo errors

@josuah josuah requested a review from avolmat-st October 11, 2025 20:14
avolmat-st
avolmat-st previously approved these changes Oct 12, 2025
Copy link

@avolmat-st avolmat-st left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks a lot !!

@avolmat-st
Copy link

@jfischer-no, @tmon-nordic, could you have a look at this PR which is a dependency for some others ? Thanks.

* At runtime, it will forward all USB controls from the host to this device.
* It will query its supported video controls and frame intervals and use this information to
* generate the USB descriptors presented to the host. At runtime, it will forward all USB controls
* from the host to this device.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At runtime, it will forward all USB controls from the host to this device.

How would this function "forward all USB controls" and why? I doubt this documentation is correct. Perhaps it needs different wording, also not clear how the changes are relevant to the commit message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was that the class would turn USB control commands for the UVC class into "video controls" at the Zephyr Video API level.

Good opportunity to improve the inaccurate documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that I did integrate @avolmat-st suggestions into existing commits, I just split them out like I should have.

avolmat-st
avolmat-st previously approved these changes Oct 13, 2025
@mathieuchopstm mathieuchopstm modified the milestones: 4.3, v4.3.0 Oct 14, 2025
Copy link
Contributor

@ngphibang ngphibang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some nits, otherwise LGTM.

/* Format capabilities of video_dev, used everywhere through the sample */
static struct video_caps video_caps = {.type = VIDEO_BUF_TYPE_OUTPUT};

static void app_add_format(uint32_t pixfmt, uint16_t width, uint16_t height)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

width and height parameters should be uint32_t as in video_format structure. The calls to this function (line 57 and 60) also passes uint32_t type from video_format_caps

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I see uint16_t is OK as well, uint32_t is a bit overkill. Maybe we should change the video_format_caps and video_format 's width / height fields to uint16_t in the future ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have been too used to UVC sizes... Done.

};
int ret;

/* Set the format to get the pitch */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be /* Set the format to get the size */ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the comment was not updated since fmt.size introduction. Done.

/* Set the format to get the pitch */
ret = video_set_format(video_dev, &fmt);
if (ret != 0) {
LOG_ERR("Could not set the format of %s", video_dev->name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also log the fmt (size, width, height) to give more info about which format has failed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.

void uvc_set_video_dev(const struct device *uvc_dev, const struct device *video_dev);

/**
* @brief Set the video format capabilities that a UVC instance will present to the host.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function description is confused to me. The function rather add a format, not set the video format caps, right ?

@brief Add a video format ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this got refactored and the comment did not get updated properly. Done.

Comment on lines 51 to 52
* @param uvc_dev The UVC device to configure
* @param fmt The video format to add to this UVC instance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param Pointer to ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here too.

*
* @param uvc_dev The UVC device
* @param video_dev The video device that this UVC instance controls
* @param video_dev The video device that this UVC instance send controls requests to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param Pointer to ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated here and other places.


ret = video_set_format(video_dev, &fmt);
if (ret != 0) {
LOG_WRN("Could not set the format of %s", video_dev->name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also log the fmt (size, width, height) to give more info about which format has failed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here too.

static struct video_caps video_caps = {.type = VIDEO_BUF_TYPE_OUTPUT};

static void app_add_format(uint32_t pixfmt, uint16_t width, uint16_t height)
static bool app_is_standard_format(uint32_t pixfmt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: app_is_supported_format() ? as the definition of which formats are "standard" seems not clear

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the UVC standard
USB_Video_Payload_Uncompressed_1.5.pdf

screenshot_2025_1014_2237_03

But the list is already outdated and incomplete!
I added NV12.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed GREY, added NV12, and renamed the function.

Glad to pick a different strategy!

}

/* Check whether the video device supports one of the wisespread image sensor formats */
static bool app_has_standard_formats(void)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit : app_has_supported_formats() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied there too.

For supporting situations where UVC formats are dynamically generated
at runtime, do not return an error in case descriptors were not added,
but instead verbosely log the error and allow enumeration to happen.

Signed-off-by: Josuah Demangeon <[email protected]>
Make use of the recently merged fmt->size to know the maximum size of the
frame to be allocated, which works for both compressed and uncompressed
video.

Signed-off-by: Josuah Demangeon <[email protected]>
The UVC class was deciding itself which formats were sent to the host.
Remove this logic out of the UVC class and introduce uvc_add_format() to
give the application the freedom of which format to list.

Signed-off-by: Josuah Demangeon <[email protected]>
The UVC class now lets the application select the format list sent to the
host. Leverage this in the sample to filter out any format that is not
expected to work (buffer too big, rarely supported formats).

Signed-off-by: Josuah Demangeon <[email protected]>
Add USB UVC device's new uvc_add_format() function to the release note,
and document the semantic changes of UVC APIs in the migration guide.

Signed-off-by: Josuah Demangeon <[email protected]>
Copy link
Contributor Author

@josuah josuah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force-push:

/* Format capabilities of video_dev, used everywhere through the sample */
static struct video_caps video_caps = {.type = VIDEO_BUF_TYPE_OUTPUT};

static void app_add_format(uint32_t pixfmt, uint16_t width, uint16_t height)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have been too used to UVC sizes... Done.

};
int ret;

/* Set the format to get the pitch */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the comment was not updated since fmt.size introduction. Done.

/* Set the format to get the pitch */
ret = video_set_format(video_dev, &fmt);
if (ret != 0) {
LOG_ERR("Could not set the format of %s", video_dev->name);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.


ret = video_set_format(video_dev, &fmt);
if (ret != 0) {
LOG_WRN("Could not set the format of %s", video_dev->name);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here too.

void uvc_set_video_dev(const struct device *uvc_dev, const struct device *video_dev);

/**
* @brief Set the video format capabilities that a UVC instance will present to the host.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this got refactored and the comment did not get updated properly. Done.

*
* @param uvc_dev The UVC device
* @param video_dev The video device that this UVC instance controls
* @param video_dev The video device that this UVC instance send controls requests to
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated here and other places.

Comment on lines 51 to 52
* @param uvc_dev The UVC device to configure
* @param fmt The video format to add to this UVC instance
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here too.

static struct video_caps video_caps = {.type = VIDEO_BUF_TYPE_OUTPUT};

static void app_add_format(uint32_t pixfmt, uint16_t width, uint16_t height)
static bool app_is_standard_format(uint32_t pixfmt)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the UVC standard
USB_Video_Payload_Uncompressed_1.5.pdf

screenshot_2025_1014_2237_03

But the list is already outdated and incomplete!
I added NV12.

static struct video_caps video_caps = {.type = VIDEO_BUF_TYPE_OUTPUT};

static void app_add_format(uint32_t pixfmt, uint16_t width, uint16_t height)
static bool app_is_standard_format(uint32_t pixfmt)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed GREY, added NV12, and renamed the function.

Glad to pick a different strategy!

}

/* Check whether the video device supports one of the wisespread image sensor formats */
static bool app_has_standard_formats(void)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied there too.

Copy link
Contributor Author

@josuah josuah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@josuah josuah requested a review from ngphibang October 14, 2025 21:02
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Samples Samples area: USB Universal Serial Bus area: Video Video subsystem Release Notes Required Release notes required for this change Release Notes To be mentioned in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants